#ifndef _BTREE_H
#define _BTREE_H
#include "type.h"

struct b_tree
{
	struct b_tree *r;
	struct b_tree *l;
	void *data;
};

/*
 * Callback function to compare agrument p1 and p2
 * p1 < p2, return -1
 * p1 = p2, return 0
 * p1 > p2, return 1
 */
typedef i_int32_t (*btree_cmp)(void *p1, void *p2, void *arg);

/*
 * Callback function to allocate agrument for binary tree
 */
typedef void * (*btree_alloc) (void *arg);

/*
 * Callback function to free memory of the binary data
 */
typedef void (*btree_free)(void *arg);

/*
 * Callback function to deal with data if the key exist in the tree
 */
typedef void (*btree_equal) (void *);

struct root_node
{
	btree_cmp cmp;
	btree_alloc data_alloc;
	btree_free data_free;
	btree_equal deal_equal;
	struct b_tree *head;
};

/* Only the equal func can be NULL */
bool btree_init (struct root_node *root, btree_cmp cmp,
		btree_alloc data_alloc, btree_free data_free, btree_equal equal);

bool btree_insert (struct root_node *root, void *arg);

void btree_delete (struct root_node *root, void *arg);

void btree_delete_tree (struct root_node *root);

bool btree_is_empty (struct root_node *root);

#endif /* _BTREE_H */
